package com.zhb.controller;

import com.zhb.beans.AjaxResult;
import com.zhb.beans.SourcesPhoto;
import com.zhb.service.SourcesPhotoService;
import com.zhb.util.Constant;
import com.zhb.util.UploadFile;
import org.activiti.engine.repository.Model;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @program: SourceAPI
 * @description:
 * @author: rainboz
 * @create: 2019-10-24 14:39
 **/
@RequestMapping("/save")
@Controller
public class FileController {

    @Autowired
    private SourcesPhotoService sourcesPhotoService;

    @RequestMapping("/images")
    public String list(Map<String, Object> map) {
        List<SourcesPhoto> imgList = sourcesPhotoService.getImgList();

        map.put("imgList", imgList);

        System.out.println(imgList);
        return "list";
    }

    @ResponseBody
    @RequestMapping("/saveImg")
    public String saveImg(MultipartFile upload) {
        sourcesPhotoService.upLoadFile(upload);
        return "ok";
    }

    @ResponseBody
    @RequestMapping("/savePhoto")
    public String save(MultipartFile file, SourcesPhoto sourcesPhoto) {
        sourcesPhotoService.save(file, sourcesPhoto);
        return "ok";
    }


    @RequestMapping(value = "/upjava", method = RequestMethod.POST)
    public String added(SourcesPhoto sourcesPhoto, @RequestParam(value = "file_adimg", required = true) MultipartFile file, HttpServletRequest request) {

        String adimg = UploadFile.upimg(file, request, "java");
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String format = sdf.format(new Date());
        long size = file.getSize();
        String originalFilename = file.getOriginalFilename();

        //错误回显：去往error.jsp需要区分错误类型

        System.out.println("=============" + adimg);
        if (!adimg.isEmpty()) {

            sourcesPhoto.setImgName(originalFilename);
            sourcesPhoto.setImgUrl(adimg);
            sourcesPhoto.setImgSize(size);
            sourcesPhoto.setGmtCreate(format);
            sourcesPhoto.setGmtModified(format);

            //过滤规则，重名图片不能提交，
            boolean img_flag = sourcesPhotoService.selectPohtoByName(originalFilename);
            String extensionName = UploadFile.getExtensionName(file);

            if (img_flag) {
                //图片可上传
                sourcesPhotoService.saveImg(file, sourcesPhoto);

                //成功转发向主页：问题转发后浏览器上的url不变，导致第二次上传图片请求的url错误，这里应该使用重定向
                return "redirect:/upload";
            } else {
                System.out.println("图片名已重复");
                request.setAttribute("error_msg", Constant.IMG_ALREADY);
                return "orther/error";
            }
        } else {
            //扩展名不符合图片
            System.out.println("扩展名不符合图片");
            request.setAttribute("error_msg", Constant.IMG_ExtensionName);
            return "orther/error";
           /* //未选择文件
            System.out.println("未选择文件");
            request.setAttribute("error_msg", Constant.IMG_NO_Choose);
            return "orther/error";*/
        }
    }


}
